package com.sanery.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sanery.dao.ListDaoImpl;
import com.sanery.entity.User;
import com.sanery.util.UploadHelper;

@WebServlet("/add")
public class AddWrongServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/pages/add.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("cur_user");

		List list = new ArrayList();
		list.add(user.getId());

		try {
			String prjdir = request.getServletContext().getRealPath("/"); // D:\APP\Java\apache-tomcat-8.5.11\webapps\ErrorRegister\
			String dir = new File(prjdir).getParent() + "\\imagesdir"; // D:\APP\Java\apache-tomcat-8.5.11\webapps\imagesdir

			Map<String, String> map = UploadHelper.upload2(request, dir, 10485760, ".jpg.png.gif.bmp");
			list.add(map.get("title"));
			list.add(map.get("img"));
			list.add(map.get("answer"));
			list.add(map.get("tip"));
			list.add(map.get("dealtype"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		ListDaoImpl dao = new ListDaoImpl();
		dao.addWrong(list);
		response.sendRedirect("/ErrorRegister/list");
	}

}
